﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dathuc
{
    public class DaThuc
    {
        public List<DonThuc> donthuc;
        private int length; //so don thuc
        public int Length
        {
            get { return length; }
        }

        public DaThuc()
        {
            donthuc = new List<DonThuc>();
            length = 0;
        }

        public DaThuc(string bieuthuc)
        {
            donthuc = new List<DonThuc>();
            length = 0;
            string[] ds;
            ds = bieuthuc.Split('+');
            foreach (string dt in ds)
            {
                donthuc.Add(new DonThuc(dt));
                ++length;
            }
        }

        public void Add(DonThuc dt)
        {
            if (length == 0)
                donthuc = new List<DonThuc>();
            donthuc.Add(dt);
            ++length;
        }

        public static DaThuc operator +(DaThuc dt1, DaThuc dt2)
        {
            DaThuc temp = new DaThuc();
            int i, j;
            //dua da thuc 1 vao temp
            for (i = 0; i < dt1.Length; i++)
                temp.Add(dt1.donthuc[i]);
            //cong da thuc hai vao temp
            for (i = 0; i < dt2.Length; i++)
            {
                for (j = 0; j < temp.Length; j++) //tim don thuc trong temp co cung so mu voi dt2
                    if (dt2.donthuc[i].Somu == temp.donthuc[j].Somu)
                    {
                        temp.donthuc[j].Heso += dt2.donthuc[i].Heso;
                        break;
                    }
                if (j == temp.Length) //khong tim thay don thuc trong temp co cung so mu voi dt2
                    temp.Add(dt2.donthuc[i]); // them mot don thuc vao temp
            }

            //sap xep lai da thuc theo so mu giam dan
            temp.Rutgon();
            temp.Sort();
            return temp;
        }

        public static DaThuc operator *(DaThuc dt1, DaThuc dt2)
        {
            DaThuc temp = new DaThuc();

            for (int i = 0; i < dt1.Length; i++)
                for (int j = 0; j < dt2.Length; j++)
                {
                    string t = (dt1.donthuc[i].Heso * dt2.donthuc[j].Heso).ToString();
                    t += "x^";
                    t += (dt1.donthuc[i].Somu + dt2.donthuc[j].Somu).ToString();
                    temp.Add(new DonThuc(t));
                }
            temp.Rutgon();
            temp.Sort();
            return temp;

        }

        public void Rutgon()
        {
            for(int i = 0; i < this.Length - 1; i++)
            {
                for(int j = i+ 1; j <this.Length; j++)
                {
                    if(this.donthuc[i].Somu == this.donthuc[j].Somu)
                    {
                        this.donthuc[i] = this.donthuc[i] + this.donthuc[j];
                        this.donthuc.RemoveAt(j);
                        length--; //giam so don thuc di 1
                    }
                }
            }
        }


        public void Sort()    //sap xep lai da thuc theo so mu giam dan
        {
            this.donthuc.Sort(delegate (DonThuc a, DonThuc b)
            {
                if (a.Somu == b.Somu) return 0;
                if (a.Somu > b.Somu) return -1;
                else return 1;
            });
        }

        public override string ToString()
        {
            if (this.Length == 0)
                return "";

            string chuoi = "";
            for (int i = 0; i < this.Length; i++)
            {
                chuoi += this.donthuc[i].ToString();
                if (i + 1 < this.Length)
                {
                    if (this.donthuc[i + 1].Heso > 0)
                        chuoi += " + ";
                }
            }
            return chuoi;
        }
    }
}
